<template>
  <div class="waterfall-grid">
    <div v-for="(item, index) in postList" :key="index" class="container">
      <div class="image-wrapper">
        <video v-if="item.postPhoto.split('.').pop()==='mp4'" :src="item.postPhoto" controls @click="commentDetails(item)" />
        <img v-else :src="item.postPhoto" alt="��ͼƬ" @click="commentDetails(item)">
        <span class="title">{{ item.postTitle }}</span>
      </div>
      <div class="user-info">
        <img class="user-icon" :src="item.photo" alt="�û�ͼ��">
        <span>{{ item.name }}</span>
        <div class="checkbox-wrapper">
          <input id="like-checkbox" v-model="item.isPraise" type="checkbox" class="checkbox">
          <svg width="24" height="24" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" class="heart-icon" @click="praisePost(item)">
            <path :d="heartPath" :fill="item.isPraise ? 'red' : 'gray'" />
          </svg>
          <span class="like-count">{{ item.postPraise }}</span>
        </div>
      </div>
    </div>
    <blog-detall ref="postDetails" />
  </div>
</template>
<script>
import { getAction, postAction } from '@/utils/xhs-ajax'
import blogDetall from '@/views/blogManager/detall/blogDetall.vue'

export default {
  components: {
    blogDetall
  },
  data() {
    return {
      postList: [],
      heartPath: 'M12 21.35l-1.45-1.32C5.4 15.36 2 12.28 2 8.5 2 5.42 4.42 3 7.5 3c1.74 0 3.41.81 4.5 2.09C13.09 3.81 14.76 3 16.5 3 19.58 3 22 5.42 22 8.5c0 3.78-3.4 6.86-8.55 11.54L12 21.35z' // ����ͼ���·������
    }
  },
  created() {
    this.getPostList()
  },
  methods: {
    praisePost(item) {
      var id = item.postId
      item.isPraise = !item.isPraise
      if (item.isPraise) {
        item.postPraise++
      } else {
        item.postPraise--
      }
      postAction('/post/praisePost.do', { postId: id }).then(resp => {
        this.getPostList()
        console.log(resp.data)
      })
    },
    getPostList() {
      getAction('/post/checkUserPraisePost.do', {}).then(resp => {
        console.log(resp.data)
        this.postList = resp.data
      })
    },
    commentDetails(item) {
      console.log(item)
      getAction('/post/commentDetails.do?postId=' + item.postId, {}).then(resp => {
        this.$refs.postDetails.commentList = resp.data
      })
      this.$refs.postDetails.dialogVisible = true
      this.$refs.postDetails.post = item
    }
  }
}
</script>

<style scoped lang="scss">
.waterfall-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); // ʹ��CSS Grid���Զ���书�ܣ�ÿ����С���Ϊ250px
  gap: 10px; // ������֮��ļ��
}
:root {
  --main-border-color: #ccc;
  --margin-size: 10px;
}

.container {
  width: 300px;
  height: auto;
  border: 1px solid var(--main-border-color);
  padding: var(--margin-size);
  box-sizing: border-box;
}

.image-wrapper img {
  width: 300px;
  height: 300px;
  border-bottom: 1px solid var(--main-border-color);
  margin-bottom: var(--margin-size);
  border-radius: 5%;
}
.image-wrapper video {
  width: 300px;
  height: 300px;
  border-bottom: 1px solid var(--main-border-color);
  margin-bottom: var(--margin-size);
  border-radius: 5%;
}

.title {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: var(--margin-size);
}

.user-info {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: var(--margin-size);
}

.user-icon {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  margin-right: 5px;
}

.user-info span {
  font-size: 14px;
}

.checkbox-wrapper {
  position: relative;
  display: inline-block;
  margin-left: auto;
}

.checkbox-wrapper input[type="checkbox"] {
  display: none;
}

.heart-icon {
  cursor: pointer;
}

.like-count {
  font-size: 14px;
  color: #888;
  margin-left: 5px;
}
</style>
